/* Copyright (c) 2010-2020 Vadim Surov.
 */
package com.srv.client.widget;

import java.util.Arrays;

import com.sencha.gxt.core.client.Style.SelectionMode;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.dnd.core.client.DndDragStartEvent;
import com.sencha.gxt.dnd.core.client.ListViewDragSource;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.ListView;
import com.sencha.gxt.widget.core.client.ListViewCustomAppearance;
import com.sencha.gxt.widget.core.client.container.AccordionLayoutContainer;
import com.srv.shared.Card;
import com.srv.shared.CardProperties;
import com.srv.shared.Control;
import com.srv.shared.ControlProperties;
import com.srv.shared.Set;
import com.srv.shared.SetProperties;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Widget;

public class ToolsContainer  extends AccordionLayoutContainer {

	interface Resources extends ClientBundle {
		@Source("ListView.css")
		Style css();
	}

	interface Style extends CssResource {
		String over();
		String select();
		String thumb();
		String thumbWrap();
	}

	public ToolsContainer() {

		setExpandMode(ExpandMode.SINGLE_FILL);

		AccordionLayoutAppearance appearance = GWT.<AccordionLayoutAppearance> create(AccordionLayoutAppearance.class);

		ContentPanel cp0 = new ContentPanel(appearance);
		cp0.setAnimCollapse(false);
		cp0.setHeadingText("Set");
		cp0.add(getSetListView());
		add(cp0);

		ContentPanel cp1 = new ContentPanel(appearance);
		cp1.setAnimCollapse(false);
		cp1.setHeadingText("Card");
		cp1.add(getCardListView());
		add(cp1);

		ContentPanel cp2 = new ContentPanel(appearance);
		cp2.setAnimCollapse(false);
		cp2.setHeadingText("Control");
		cp2.add(getControlListView());
		add(cp2);

		setActiveWidget(cp0);		
	}

	private Widget getControlListView() {

		final ControlProperties props = GWT.create(ControlProperties.class);

		final ListStore<Control> store = new ListStore<Control>(props.key()) {{

			add(new Control(Control.Type.Text, "", "Text"));
			add(new Control(Control.Type.Property, "Name", "New"));
			add(new Control(Control.Type.Property, "Type", "Quiz"));
			add(new Control(Control.Type.Property, "Description", ""));			
			add(new Control(Control.Type.Property, "Tags", ""));
			add(new Control(Control.Type.Image, "", "Image"));
			add(new Control(Control.Type.Radio, "checked", "Option"));
			add(new Control(Control.Type.Radio, "unchecked", "Option"));
			add(new Control(Control.Type.Checkbox, "checked", "Option"));
			add(new Control(Control.Type.Checkbox, "unchecked", "Option"));
			add(new Control(Control.Type.Score, "", "100%"));
			add(new Control(Control.Type.Line, "", ""));
		}};

		final Resources resources = GWT.create(Resources.class);
		resources.css().ensureInjected();
		final Style style = resources.css();

		final ListView<Control, String> listView = new ListView<Control, String>(store, props.getToolIcon(), 
				new ListViewCustomAppearance<Control>("." + style.thumbWrap(), style.over(), style.select()) 
				{
					@Override
					public void renderItem(SafeHtmlBuilder builder, SafeHtml content) {
						builder.appendHtmlConstant("<div class='" + style.thumbWrap() + "' style='border: 1px solid white'>");
						builder.append(content);
						builder.appendHtmlConstant("</div>");
					}
				}
				); 

		listView.setCell(new AbstractCell<String>() {
			@Override
			public void render(Context context, String value, SafeHtmlBuilder sb) {
				/*
				 * Always do a null check on the value. Cell widgets can pass null to
				 * cells if the underlying data contains a null, or if the data arrives
				 * out of order.
				 */				
				if (value == null)
					return;				
				sb.appendHtmlConstant(value);
			}
		});		
		listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		new ListViewDragSource<Control>(listView) {
			{
				setGroup("movecontrol");
			}
			@Override
			protected void onDragStart(DndDragStartEvent event) {
				super.onDragStart(event);
				// by default drag is allowed
				final Control control = listView.getSelectionModel().getSelection().get(0);
				event.getStatusProxy().update(control.getDragIcon());
			}

		};	        

		return listView;
	}

	private Widget getCardListView() {
		final CardProperties props = GWT.create(CardProperties.class);

		final ListStore<Card> store = new ListStore<Card>(props.key()) {{
			add(new Card(0, Arrays.asList(
					new Control(Control.Type.Property, "Name", "New"),
					new Control(Control.Type.Property, "Type", "Interpresentation"),
					new Control(Control.Type.Property, "Description", ""),			
					new Control(Control.Type.Property, "Tags", "")		
					)));

			add(new Card(0, Arrays.asList(
					new Control(Control.Type.Text, "", "Statement"),
					new Control(Control.Type.Radio, "unchecked", "Option 1"),			
					new Control(Control.Type.Radio, "checked", "Option 2"),			
					new Control(Control.Type.Radio, "unchecked", "Option 3")	
					)));

			add(new Card(0, Arrays.asList(
					new Control(Control.Type.Text, "", "Statement"),
					new Control(Control.Type.Checkbox, "checked", "Option 1"),	
					new Control(Control.Type.Checkbox, "unchecked", "Option 2"),			
					new Control(Control.Type.Checkbox, "checked", "Option 3")		
					)));

			add(new Card(0, Arrays.asList(
					new Control(Control.Type.Text, "", "Thank You!"),
					new Control(Control.Type.Score, "", "0%")
					)));
		}};

		final Resources resources = GWT.create(Resources.class);
		resources.css().ensureInjected();
		final Style style = resources.css();

		final ListView<Card, String> listView =	new ListView<Card, String>(store, props.getToolIcon(), 
				new ListViewCustomAppearance<Card>("." + style.thumbWrap(), style.over(), style.select()) 
				{
					@Override
					public void renderItem(SafeHtmlBuilder builder, SafeHtml content) {
						builder.appendHtmlConstant("<div class='" + style.thumbWrap() + "' style='border: 1px solid white'>");
						builder.append(content);
						builder.appendHtmlConstant("</div>");
					}
				}
				); 

		listView.setCell(new AbstractCell<String>() {
			@Override
			public void render(Context context, String value, SafeHtmlBuilder sb) {
				/*
				 * Always do a null check on the value. Cell widgets can pass null to
				 * cells if the underlying data contains a null, or if the data arrives
				 * out of order.
				 */				
				if (value == null)
					return;				
				sb.appendHtmlConstant(value);
			}
		});		
		listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		new ListViewDragSource<Card>(listView) {
			{
				setGroup("movecard");
			}
			@Override
			protected void onDragStart(DndDragStartEvent event) {
				super.onDragStart(event);
				// by default drag is allowed
				final Card card = listView.getSelectionModel().getSelection().get(0);
				event.getStatusProxy().update(card.getDragIcon());
			}

		};	        

		return listView;
	}

	private Widget getSetListView() {
		final SetProperties props = GWT.create(SetProperties.class);

		final ListStore<Set> store = new ListStore<Set>(props.key()) {{
			add(new Set(
					Arrays.asList(
							new Card(0,  
									Arrays.asList(
											new Control(Control.Type.Property, "Name", "New"),
											new Control(Control.Type.Property, "Type", "Drill"),
											new Control(Control.Type.Property, "Description", ""),			
											new Control(Control.Type.Property, "Tags", "")	
											)
									),
									new Card(0,  
											Arrays.asList(
													new Control(Control.Type.Text, "", "Statement"),
													new Control(Control.Type.Radio, "unchecked", "Option 1"),			
													new Control(Control.Type.Radio, "checked", "Option 2"),			
													new Control(Control.Type.Radio, "unchecked", "Option 3")
													)
											),
											new Card(0,  
													Arrays.asList(
															new Control(Control.Type.Text, "", "Statement"),
															new Control(Control.Type.Checkbox, "checked", "Option 1"),			
															new Control(Control.Type.Checkbox, "unchecked", "Option 2"),			
															new Control(Control.Type.Checkbox, "checked", "Option 3")
															)
													),
													new Card(0,  
															Arrays.asList(
																	new Control(Control.Type.Text, "", "<center>Thank You!</center>"),
																	new Control(Control.Type.Score, "", "0%")
																	)
															)
							)
					));

			add(new Set(
					Arrays.asList(
							new Card(0,  
									Arrays.asList(
											new Control(Control.Type.Property, "Name", "New"),
											new Control(Control.Type.Property, "Type", "Quiz"),
											new Control(Control.Type.Property, "Description", ""),			
											new Control(Control.Type.Property, "Tags", "")	
											)
									),
									new Card(0,  
											Arrays.asList(
													new Control(Control.Type.Text, "", "Statement"),
													new Control(Control.Type.Radio, "unchecked", "Option 1"),			
													new Control(Control.Type.Radio, "checked", "Option 2"),			
													new Control(Control.Type.Radio, "unchecked", "Option 3")
													)
											),
											new Card(0,  
													Arrays.asList(
															new Control(Control.Type.Text, "", "Statement"),
															new Control(Control.Type.Checkbox, "checked", "Option 1"),			
															new Control(Control.Type.Checkbox, "unchecked", "Option 2"),			
															new Control(Control.Type.Checkbox, "checked", "Option 3")
															)
													),
													new Card(0,  
															Arrays.asList(
																	new Control(Control.Type.Text, "", "<center>Thank You!</center>"),
																	new Control(Control.Type.Score, "", "0%")
																	)
															)
							)
					));

			add(new Set(
					Arrays.asList(
							new Card(0,  
									Arrays.asList(
											new Control(Control.Type.Property, "Name", "New"),
											new Control(Control.Type.Property, "Type", "Interactive Presentation"),
											new Control(Control.Type.Property, "Description", ""),			
											new Control(Control.Type.Property, "Tags", "")	
											)
									),
									new Card(0,  
											Arrays.asList(
													new Control(Control.Type.Text, "", "Statement"),
													new Control(Control.Type.Radio, "unchecked", "Option 1"),			
													new Control(Control.Type.Radio, "checked", "Option 2"),			
													new Control(Control.Type.Radio, "unchecked", "Option 3")
													)
											),
											new Card(0,  
													Arrays.asList(
															new Control(Control.Type.Text, "", "Statement"),
															new Control(Control.Type.Checkbox, "checked", "Option 1"),			
															new Control(Control.Type.Checkbox, "unchecked", "Option 2"),			
															new Control(Control.Type.Checkbox, "checked", "Option 3")
															)
													),
													new Card(0,  
															Arrays.asList(
																	new Control(Control.Type.Text, "", "<center>Thank You!</center>"),
																	new Control(Control.Type.Score, "", "0%")
																	)
															)
							)
					));

			add(new Set(
					Arrays.asList(
							new Card(0,  
									Arrays.asList(
											new Control(Control.Type.Property, "Name", "New"),
											new Control(Control.Type.Property, "Type", "Poll"),
											new Control(Control.Type.Property, "Description", ""),			
											new Control(Control.Type.Property, "Tags", "")	
											)
									),
									new Card(0,  
											Arrays.asList(
													new Control(Control.Type.Text, "", "Statement"),
													new Control(Control.Type.Radio, "unchecked", "Option 1"),			
													new Control(Control.Type.Radio, "checked", "Option 2"),			
													new Control(Control.Type.Radio, "unchecked", "Option 3")
													)
											),
											new Card(0,  
													Arrays.asList(
															new Control(Control.Type.Text, "", "Statement"),
															new Control(Control.Type.Checkbox, "checked", "Option 1"),			
															new Control(Control.Type.Checkbox, "unchecked", "Option 2"),			
															new Control(Control.Type.Checkbox, "checked", "Option 3")
															)
													),
													new Card(0,  
															Arrays.asList(
																	new Control(Control.Type.Text, "", "<center>Thank You!</center>"),
																	new Control(Control.Type.Score, "", "0%")
																	)
															)
							)
					));
		}};

		
		final Resources resources = GWT.create(Resources.class);
		resources.css().ensureInjected();
		final Style style = resources.css();
		
		final ListView<Set, String> listView = new ListView<Set, String>(store, props.getToolIcon(), 
				new ListViewCustomAppearance<Set>("." + style.thumbWrap(), style.over(), style.select()) 
				{
					@Override
					public void renderItem(SafeHtmlBuilder builder, SafeHtml content) {
						builder.appendHtmlConstant("<div class='" + style.thumbWrap() + "' style='border: 1px solid white'>");
						builder.append(content);
						builder.appendHtmlConstant("</div>");
					}
				}
				); 

		listView.setCell(new AbstractCell<String>() {
			@Override
			public void render(Context context, String value, SafeHtmlBuilder sb) {
				/*
				 * Always do a null check on the value. Cell widgets can pass null to
				 * cells if the underlying data contains a null, or if the data arrives
				 * out of order.
				 */				
				if (value == null)
					return;				
				sb.appendHtmlConstant(value);
			}
		});		
		listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);


		new ListViewDragSource<Set>(listView) {
			{
				setGroup("moveset");
			}
			@Override
			protected void onDragStart(DndDragStartEvent event) {
				super.onDragStart(event);
				// by default drag is allowed
				final Set set = listView.getSelectionModel().getSelection().get(0);
				event.setData(set);
				event.getStatusProxy().update(set.getDragIcon());
			}

		};	        

		return listView;
	}	
}
